package com.lujianfei.recyclerview.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lujianfei.recyclerview.R;
import com.lujianfei.recyclerview.entity.ListItem;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerScrollHideSearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEAD = 0;
    private static final int TYPE_ITEM = 1;

    List<ListItem> listItemList;
    private final int headCount = 1;

    public RecyclerScrollHideSearchAdapter(List<ListItem> listItemList) {
        this.listItemList = listItemList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_item, parent, false);
        RecyclerView.ViewHolder viewHolder = new ItemViewHolder(view);
        if (TYPE_HEAD == viewType) {
            viewHolder = new SearchViewHolder(view);
        } else if (TYPE_ITEM == viewType) {
            viewHolder = new ItemViewHolder(view);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (TYPE_HEAD == itemViewType) {
            SearchViewHolder searchViewHolder = (SearchViewHolder) holder;
        } else if (TYPE_ITEM == itemViewType) {
            ListItem listItem = this.listItemList.get(position - headCount);
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            itemViewHolder.title.setText(listItem.getTitle());
            itemViewHolder.summary.setText(listItem.getSummary());
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (0 == position) {
            return TYPE_HEAD;
        }
        return TYPE_ITEM;
    }

    @Override
    public int getItemCount() {
        return this.listItemList.size() + headCount;
    }

    static class SearchViewHolder extends RecyclerView.ViewHolder {
        View contentView;

        public SearchViewHolder(View view) {
            super(view);
            contentView = view;
        }
    }
    static class ItemViewHolder extends RecyclerView.ViewHolder {
        View contentView;
        TextView title;
        TextView summary;

        public ItemViewHolder(View view) {
            super(view);
            contentView = view;
            title = view.findViewById(R.id.title);
            summary = view.findViewById(R.id.summary);
        }
    }
}
